package com.syni.mdd.yhd.common.all.controller.protect.business;

/**
 * @className BmsBusinessController
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/15 14:25
 * @VERSION 1.0
 **/

import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckParams;
import com.syni.mdd.sjb.common.component.utils.common.spring.ApplicationContextHolder;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.service.BmsBusinessGroupMessageService;
import com.syni.mdd.yhd.common.all.service.BmsGroupMessageReportService;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/businessGroupMessage")
public class BmsBusinessGroupMessageController {
    @Autowired
    BmsBusinessGroupMessageService bmsBusinessGroupMessageService;

    @Autowired
    BmsGroupMessageReportService bmsGroupMessageReportService;

    @PostMapping("/createHome")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> createHome(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        return bmsBusinessGroupMessageService.createHome(userId,businessId);
    }


    @PostMapping("/createBefore")
    @CheckParams(checkAuth = true,params = {"business_id"})
    public Map<String,Object> createBefore(@RequestBody Map<String,Object> paramsMap){
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer sendType = 1;
        if(paramsMap.containsKey("send_type") && StringUtils.checkNullAndEmpty(paramsMap.get("send_type"))){
            sendType = Integer.parseInt(paramsMap.get("send_type").toString());
        }
        return bmsBusinessGroupMessageService.createBefore(userId,businessId,sendType);
    }

    @PostMapping("/create")
    @CheckParams(checkAuth = true,params = {"business_id","content"})
    public Map<String,Object> create(@RequestBody ParamsMap paramsMap){
        int GROUOP_MESSAGE_MAX = 150;
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        //默认粉丝群发
        Integer sendType = 1;
        if(paramsMap.containsKey("send_type") && StringUtils.checkNullAndEmpty(paramsMap.get("send_type"))){
            sendType = Integer.parseInt(paramsMap.get("send_type").toString());
        }
        //定时发送
        String regularlySend = null;
        if(paramsMap.containsKey("regularly_send") && StringUtils.checkNullAndEmpty(paramsMap.get("regularly_send"))){
            regularlySend = paramsMap.get("regularly_send").toString();
        }
        //是否全选
        boolean choseAll = false;
        if(paramsMap.containsKey("chose_all") && StringUtils.checkNullAndEmpty(paramsMap.get("chose_all"))){
            choseAll = true;
        }
        String sendUser = null;
        if(paramsMap.containsKey("send_user") && StringUtils.checkNullAndEmpty(paramsMap.get("send_user"))) {
            sendUser = paramsMap.get("send_user").toString();
        }
        String content = paramsMap.getString("content");
        if(content.length() > GROUOP_MESSAGE_MAX){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_GROUP_MESSAGE_CONTENT_MAX,"群发消息内容过长");
        }
        Integer activityId = paramsMap.getInteger("activityId");
        Integer businessCouponId = paramsMap.getInteger("businessCouponId");
        return bmsBusinessGroupMessageService.create(userId,businessId,sendUser,
                content,sendType,regularlySend,
                choseAll,activityId,businessCouponId);
    }

    @PostMapping("/repeatCreate")
    @CheckParams(checkAuth = true,params = {"group_message_id","business_id","content"})
    public Map<String,Object> repeatCreate(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer groupMessageId = Integer.parseInt(paramsMap.get("group_message_id").toString());
        String content = paramsMap.get("content").toString();
        //定时发送
        String regularlySend = null;
        if(paramsMap.containsKey("regularly_send") && StringUtils.checkNullAndEmpty(paramsMap.get("regularly_send"))){
            regularlySend = paramsMap.get("regularly_send").toString();
        }
        return bmsBusinessGroupMessageService.repeatCreate(userId,businessId,groupMessageId,content,regularlySend);
    }



    @PostMapping("/findSendUserDetail")
    @CheckParams(checkAuth = true,params = {"group_message_id","business_id","page_num","page_size"})
    public Map<String,Object> findSendUserDetail(@RequestBody Map<String,Object> paramsMap){
        return bmsBusinessGroupMessageService.findGroupMessageUser(paramsMap);
    }

    @PostMapping("/getBusinessGroupMessage")
    @CheckParams(checkAuth = true,params = {"business_id","page_num","page_size"})
    public Map<String,Object> getBusinessGroupMessage(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        Integer type = 1;
        if(paramsMap.containsKey("type") && StringUtils.checkNullAndEmpty(paramsMap.get("type"))){
            type = Integer.parseInt(paramsMap.get("type").toString());
        }
        return bmsBusinessGroupMessageService.findBusinessGroupMessage(userId,businessId,type,pageNum,pageSize);
    }

    @PostMapping("/getUserGroupMessage")
    @CheckParams(checkAuth = true,params = {"business_id","page_num","page_size"})
    public Map<String,Object> getUserGroupMessage(@RequestBody Map<String,Object> paramsMap){
        Integer userId = Integer.parseInt(paramsMap.get("userIdentify").toString());
        Integer businessId = Integer.parseInt(paramsMap.get("business_id").toString());
        Integer pageNum = Integer.parseInt(paramsMap.get("page_num").toString());
        Integer pageSize = Integer.parseInt(paramsMap.get("page_size").toString());
        return bmsBusinessGroupMessageService.findUserGroupMessage(userId,businessId,pageNum,pageSize);
    }



}
